package com.example.ah_geen_pda.ui.datacheck

import androidx.databinding.ObservableArrayList
import com.example.ah_geen_pda.R
import com.example.ah_geen_pda.base.BaseViewModel
import com.example.ah_geen_pda.entity.ComponentList
import com.example.ah_geen_pda.entity.LotEntity
import com.example.ah_geen_pda.entity.ToastMessageBean
import com.example.ah_geen_pda.model.LotInfoRepository
import com.glorysoft.sunnypda.entity.ui.CallObserveBean
import com.google.gson.JsonObject
import me.tatarka.bindingcollectionadapter2.BR
import me.tatarka.bindingcollectionadapter2.ItemBinding

class DataCheckViewModel: BaseViewModel() {

    var lotEntity = LotEntity()
    val loginRepository : LotInfoRepository = LotInfoRepository()
    var dataItems = ObservableArrayList<LotEntity>()
    var componentItems = ObservableArrayList<ComponentList>()
    var lotDataBinding =
        ItemBinding.of<ComponentList>(BR.dataItem, R.layout.item_data_check_list)
    var durableDataBinding =
        ItemBinding.of<LotEntity>(BR.dataItem, R.layout.item_durabledata_check_list)

    fun getLotInfo(lotid:String,b:Boolean){
        val map = JsonObject()
        if (b){
            map.addProperty("ACTIONTYPE","LOTINFOBYDURABLE")
            map.addProperty("DURABLE",lotid)
        }else{
            map.addProperty("ACTIONTYPE","LOTINFOBYID")
            map.addProperty("LOTID",lotid)
        }
        launchOnlyResult({
            dataItems.clear()
            componentItems.clear()
            lotEntity = loginRepository.getLotInfo(map)
            for (i in 0..lotEntity.LOT.COMPONENTLIST.size-1) {
                lotEntity.LOT.COMPONENTLIST[i].post = i+1
            }
            if (!componentItems.containsAll(lotEntity.LOT.COMPONENTLIST)){
                componentItems.addAll(lotEntity.LOT.COMPONENTLIST)
                lotEntity.LOT.start = componentItems[0].ATTRIBUTE1
                lotEntity.LOT.end = componentItems[componentItems.size-1].ATTRIBUTE1
                dataItems.add(lotEntity)
            }
            defUI.callObserve.value = CallObserveBean(0,lotEntity)
        })
    }
}